using Microsoft.AspNetCore.Mvc;
using MediatR;
using B.S.BasicData.EroorCode;
using B.S.BasicData.Domain;

namespace B.S.BasicData.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeepProcessingController : ControllerBase
    {
        private readonly IMediator _mediator;

        public DeepProcessingController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 深加工信息连表查询（深加工信息 + 深加工企业信息 + 供应商信息）
        /// </summary>
        /// <param name="batchNo">生产批次</param>
        /// <returns>连表查询结果</returns>
        [HttpGet]
        public async Task<IActionResult> JoinQuery([FromQuery] string batchNo)
        {
            var request = new DeepProcessingJoinQueryRequest { BatchNo = batchNo };
            var result = await _mediator.Send(request);
            return Ok(result);
        }

        /// <summary>
        /// 根据DeepProcessingInfo主键查询连表数据
        /// </summary>
        /// <param name="id">DeepProcessingInfo的主键ID</param>
        /// <returns>连表查询结果</returns>
        [HttpGet]
        public async Task<IActionResult> GetById([FromQuery] int id)
        {
            if (id <= 0)
            {
                return BadRequest(ApiResult<DeepProcessingJoinDto>.Fail("无效的ID", ApiResultCode.Error));
            }

            var request = new DeepProcessingJoinGetByIdRequest { Id = id };
            var result = await _mediator.Send(request);
            return Ok(result);
        }

        /// <summary>
        /// 修改深加工信息
        /// </summary>
        /// <param name="request">修改请求</param>
        /// <returns>修改结果</returns>
        [HttpPut]
        public async Task<IActionResult> Update([FromBody] DeepProcessingInfoUpdateRequest request)
        {
            if (request == null)
            {
                return BadRequest(ApiResult<DeepProcessingInfo>.Fail("请求参数不能为空", ApiResultCode.Error));
            }

            var result = await _mediator.Send(request);
            return Ok(result);
        }
    }
} 